package com.jrx.anytxn.reporting.job.xib.step;

import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.entity.CmInteCoLonRepayDtlCi;
import com.jrx.anytxn.reporting.service.IXibService;
import com.jrx.anytxn.transaction.entity.AmLoanAccountInfo;
import com.jrx.anytxn.transaction.entity.TlPaymentLog;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @comment: 贷款信息Processor
 * @author: liuzb
 * @date: 2021/11/27 10:16
 */
@Service
public class RepayDtlCiProcessor implements ItemProcessor<CmInteCoLonRepayDtlCi, CmInteCoLonRepayDtlCi> {



    @Autowired
    private IXibService iXibService;

    private static final Logger logger = LoggerFactory.getLogger(RepayDtlCiProcessor.class);


    @Override
    public CmInteCoLonRepayDtlCi process(CmInteCoLonRepayDtlCi repayDtlCi) throws Exception {
        try {
            if (null != repayDtlCi || StringUtils.isNotBlank(repayDtlCi.getDubilNo())) {
                return iXibService.assemRepayDtlCi(repayDtlCi);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("RepayDtlCiProcessor 出错了：{}", e.getMessage());
        }
        return null;
    }

    /**
     * 转换还款类型
     *
     * @param paymentLogList
     * @param countAcct
     * @return
     */
    private String getRepayType(List<TlPaymentLog> paymentLogList, AmLoanAccountInfo countAcct) {

        for (TlPaymentLog x : paymentLogList) {
            if (ReportingConstant.REPAYMENT_TYPE_PREPAY_CURR.equals(x.getPaymentType()) ||
                    ReportingConstant.REPAYMENT_TYPE_PREPAY.equals(x.getPaymentType()) ||
                    ReportingConstant.REPAYMENT_TYPE_PRETERMINTION.equals(x.getPaymentType()) ||
                    ReportingConstant.REPAYMENT_TYPE_BILLPREPAYCURR.equals(x.getPaymentType())
            ) {
                if (BigDecimal.ZERO.compareTo(countAcct.getCurrBalance()) < 0) {
                    return ReportingConstant.REPAYMENT_TYPE_PREPAY;
                }
                return ReportingConstant.REPAYMENT_TYPE_PRETERMINTION;
            }
        }
        return ReportingConstant.REPAYMENT_TYPE_PAY;
    }

}
